﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using MM.DBUtility;
using MM.Common;

namespace WebMMS.Manager
{
    public partial class AppurlAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["CurUser"] == null)
                {
                    Response.Redirect("~/Login.aspx");
                    return;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string strTitle = txtTitle.Text.Trim();
                if (string.IsNullOrEmpty(strTitle))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "标注不能为空！");
                    return;
                }
                string strFilePath = fuFile.FileName;
                if (string.IsNullOrEmpty(strFilePath))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "请选择文件！");
                    return;
                }
                if (!System.IO.Path.GetExtension(strFilePath).ToLower().Equals(".apk"))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "只能上传apk文件！");
                    return;
                }

                string strUPFilePath = HttpContext.Current.Server.MapPath("~/ApkFile/" + strFilePath);
                if (File.Exists(strUPFilePath))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "存在相同apk文件名，请更换文件名！");
                    return;
                }

                fuFile.SaveAs(strUPFilePath);

                string strSQL = "INSERT INTO T_Task(Type,Name,Value) VALUES(2,@Name,@Value)";
                List<SqlParameter> parameters = new List<SqlParameter>();
                parameters.Add(SqlHelper.CreateInParameter("@Name", SqlDbType.NVarChar, 500, strTitle));
                parameters.Add(SqlHelper.CreateInParameter("@Value", SqlDbType.VarChar, 500, CommonFun.ApkFilePath + strFilePath));
                MM.DBUtility.DbHelperSql.ExecuteSql(strSQL, parameters.ToArray());
                MM.Common.Common.PageAlertAndRedirect(this.Page, "添加成功！", "Appurl.aspx");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}